from django.db import models
from django.contrib.auth.hashers import make_password, check_password


class Permission(models.Model):
    """
    权限表
    """
    LEVEL = (
        (0, '一级'),
        (1, '二级'),
        (2, '三级'),
        (3, '四级'),
        # (4, '五级'),
        # (5, '六级'),
    )
    name = models.CharField(verbose_name='权限名称', max_length=64)
    path = models.CharField(verbose_name='权限路径', max_length=256)
    level = models.IntegerField(verbose_name='权限层级', default=0, choices=LEVEL)
    parent = models.ForeignKey(verbose_name='权限父级', to='self', related_name='children', null=True, blank=True, on_delete=models.CASCADE, db_constraint=False)
    parent_path = models.CharField(verbose_name='父级路径', null=True, blank=True, max_length=64)
    icon = models.CharField(verbose_name='图标', max_length=32, blank=True, null=True)
    sort = models.IntegerField(verbose_name='排序', default=999)
    create_at = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    def __str__(self):
        return "{}[{}]".format(self.name, self.get_level_display())

    class Meta:
        verbose_name = '权限管理'
        verbose_name_plural = verbose_name
        ordering = ['level', 'sort']


class Role(models.Model):
    """
    角色
    """
    name = models.CharField(verbose_name='角色名称', max_length=32)
    comment = models.CharField(verbose_name='角色说明', max_length=128, blank=True, null=True)
    permissions = models.ManyToManyField(verbose_name='角色权限', to='Permission', blank=True, db_constraint=False)
    only_own = models.BooleanField(verbose_name='只看自己内容', default=True)
    update_at = models.DateTimeField(verbose_name='创建时间', auto_now=True)
    create_at = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '角色管理'
        verbose_name_plural = verbose_name
        ordering = ['-create_at']


class UserInfo(models.Model):
    """
    用户表
    """
    name = models.CharField(verbose_name='登录名', max_length=32, unique=True)
    real_name = models.CharField(verbose_name='姓名', max_length=32)
    gender = models.CharField(verbose_name="性别",  max_length=4, choices=(("男", "男"), ("女", "女")), blank=True, null=True)
    password = models.CharField(verbose_name='密码', max_length=128, blank=True, null=True)
    telephone = models.CharField(verbose_name='手机', max_length=16)
    is_superuser = models.BooleanField(verbose_name='超级用户', default=False)
    is_active = models.BooleanField(verbose_name='是否有效', default=True)
    avatar = models.ImageField(verbose_name="头像", blank=True, null=True, upload_to="users/")
    create_at = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    last_login = models.DateTimeField(verbose_name='最近登录时间', blank=True, null=True)
    roles = models.ManyToManyField(verbose_name='所属角色', to='Role', blank=True, db_constraint=False)
    openid = models.CharField(verbose_name='微信ID', max_length=120, blank=True, null=True)

    def __str__(self):
        return self.real_name if self.real_name else self.name

    class Meta:
        verbose_name = '用户管理'
        verbose_name_plural = verbose_name
        ordering = ['-create_at']

    def set_password(self, raw_password):
        self.password = make_password(raw_password)

    def check_password(self, raw_password):
        return check_password(raw_password, self.password)


class UserLog(models.Model):
    """用户登录日志"""
    username = models.CharField(verbose_name='登录用户', max_length=32, blank=True, null=True)
    ip_address = models.CharField(verbose_name='登录IP', max_length=32, blank=True, null=True)
    desc = models.CharField(verbose_name="登录状态", max_length=32, blank=True, null=True)
    login_time = models.DateTimeField(verbose_name="登录时间", blank=True, null=True)

    class Meta:
        verbose_name = '登录日志'
        verbose_name_plural = verbose_name
        ordering = ["-login_time"]

    def __str__(self):
        return self.username
